﻿using System;
using System.Data;
using System.Data.Common;
using Gumini.JulaDB.DataTypes;
using Gumini.JulaDB.Enums;

namespace Gumini.JulaDB.Provider
{
    public class JulaDbParameter : DbParameter
    {
        DataType _dataType;

        #region Constructors

        public JulaDbParameter()
        {
            this.SourceVersion = DataRowVersion.Current;
            this.ParameterName = String.Empty;
            this.SourceColumn = String.Empty;
            this.IsNullable = false;
            this.Value = null;
        }
        
        public JulaDbParameter(string name, JulaDbType dataType)
            : this()
        {
            this.ParameterName = name;
            this.JulaDbType = dataType;
        }

        public JulaDbParameter(string name, object value)
            : this()
        {
            this.ParameterName = name;
            this.Value = value;
        }

        public JulaDbParameter(string name, JulaDbType dataType, int size)
            : this()
        {
            this.ParameterName = name;
            this.JulaDbType = dataType;
            this.Size = size;
        }

        public JulaDbParameter(string name, JulaDbType dataType, int size, string srcColumn)
            : this()
        {
            this.ParameterName = name;
            this.JulaDbType = dataType;
            this.Size = size;
            this.SourceColumn = srcColumn;
        }

        public JulaDbParameter(string parameterName, JulaDbType dbType, int size, ParameterDirection direction, bool isNullable,
            byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value)
            : this()
        {
            this.ParameterName = parameterName;
            this.JulaDbType = dbType;
            this.Size = size;
            this.Direction = direction;
            this.IsNullable = isNullable;
            this.Precision = precision;
            this.Scale = scale;
            this.SourceColumn = srcColumn;
            this.SourceVersion = srcVersion;
            this.Value = value;
        }

        #endregion

        /// <summary>
        /// Resets the DbType property to its original settings.
        /// </summary>
        public override void ResetDbType()
        {
            _dataType = null;
        }

        public override DbType DbType
        {
            get { return _dataType.DbType != null ? _dataType.DbType : (DbType)-1; }
            set { _dataType = DataType.GetType(value); }
        }

        public JulaDbType JulaDbType
        {
            get { return _dataType != null ? _dataType.Id : (JulaDbType)-1; }
            set { _dataType = DataType.GetType(value); }
        }

        /// <summary>
        /// Only input parameters are supported.
        /// </summary>
        public override ParameterDirection Direction
        {
            get { return ParameterDirection.Input; }
            set
            {
                if (value != ParameterDirection.Input)
                    throw new NotSupportedException("Only input parameters are supported");
            }
        }

        public override bool IsNullable { get; set; }

        public override string ParameterName { get; set; }

        public override int Size { get; set; }

        public override string SourceColumn { get; set; }

        public override bool SourceColumnNullMapping { get; set; }

        public override DataRowVersion SourceVersion { get; set; }

        public override object Value { get; set; }

        public byte Precision { get; set; }

        public byte Scale { get; set; }
    }
}
